package com.gitee.hljdrl.lite.app;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

import com.ffsky.logger.L;
import com.gitee.hljdrl.lite.Lite;
import com.gitee.hljdrl.lite.privacy.PrivacyUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public abstract class LiteLifecycle implements AppInterface {

    protected  String TAG = "LiteLifecycle";
    /**
     * 后台运行
     */
    private boolean mBackgrounder = false;
    protected final AtomicInteger mAtomicInteger = new AtomicInteger(0);
    protected final List<Activity> mActivityList = new ArrayList<>();

    protected Activity mResumedActivity;

    public LiteLifecycle() {

    }
    public LiteLifecycle(String tag) {
        this.TAG = tag;
    }


    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        if (activity != null) {
            mResumedActivity = activity;
            mActivityList.add(activity);
            String clsName = activity.getClass().getName();
            L.i(TAG, "Created Activity->", clsName, " ,backgrounder=", Boolean.toString(mBackgrounder));
        }
    }

    public Activity getCurrentActivity() {
        return mResumedActivity;
    }

    @Override
    public void onActivityStarted(Activity activity) {
        mAtomicInteger.incrementAndGet();
        int refCount = mAtomicInteger.get();
        if (refCount > 0) {
            setBackgrounder(false);
        }
        L.i(TAG, "Started->Count:", String.valueOf(refCount),
                ", backgrounder=", Boolean.toString(mBackgrounder));
    }

    @Override
    public void onActivityResumed(Activity activity) {
        String clsName = activity.getClass().getSimpleName();
        L.i(TAG, "ActivityResumed->", clsName);
        mResumedActivity = activity;
    }

    @Override
    public void onActivityPaused(Activity activity) {
        String name = activity.getClass().getSimpleName();
        L.i(TAG, "ActivityPaused->", name);
    }

    @Override
    public void onActivityStopped(Activity activity) {
        mAtomicInteger.decrementAndGet();
        int refCount = mAtomicInteger.get();
        if (refCount == 0) {
            setBackgrounder(true);
            mResumedActivity = null;
        } else if (refCount > 0) {
            setBackgrounder(false);
        }
        String name = activity.getClass().getSimpleName();
        L.i(TAG, "ActivityStopped->", name, " Stopped->Count:", String.valueOf(refCount),
                ", backgrounder=", Boolean.toString(mBackgrounder));
    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
    }

    @Override
    public void onActivityDestroyed(Activity activity) {
        mActivityList.remove(activity);
        int size = mActivityList.size();
        String name = activity.getClass().getSimpleName();
        L.i(TAG, "ActivityDestroyed->", name, " activityCount=", size);
    }

    public boolean isBackgrounder() {
        return mBackgrounder;
    }

    public void setBackgrounder(boolean mBackgrounder) {
        this.mBackgrounder = mBackgrounder;
    }

    public List<Activity> getActivityList() {
        List<Activity> copy = new ArrayList<>();
        if(!mActivityList.isEmpty()){
            copy.addAll(mActivityList);
        }
        return copy;
    }


    @Override
    public boolean isAgreePrivacyAgreement() {
        return PrivacyUtil.isAccept(Lite.getContext());
    }

    @Override
    public void agreePrivacyAgreement() {
        PrivacyUtil.accept(Lite.getContext());
    }
}
